package com.gljx.web.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gljx.constant.ActivitiKey;
import com.gljx.util.ActitiviUtil;
import com.gljx.util.Axamine;
import com.gljx.util.ServerResponse;
import org.activiti.rest.common.api.ActivitiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gljx.util.GetFid;
import com.gljx.web.Entity.Car;
import com.gljx.web.Entity.InsuranceReg;
import com.gljx.web.Entity.InsuranceRegEntry;
import com.gljx.web.Entity.User;
import com.gljx.web.dao.CarMapper;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.dao.InsuranceRegEntryMapper;
import com.gljx.web.dao.InsuranceRegMapper;
import com.gljx.web.service.InsuranceRegService;

@Service
@Transactional
public class InsuranceRegServiceImpl extends CommonDao implements InsuranceRegService{

    //导入activiti工具
    ActitiviUtil activitiUtil = new ActitiviUtil();
    @Autowired
	private InsuranceRegMapper insuranceRegMapper;
	@Autowired
	private InsuranceRegEntryMapper insuranceRegEntryMapper;
	@Autowired
	private CarMapper carMapper;
	
	/**
	 *取分页数据    dmz 20160921
	 *@param pindex 页码
	 *@param psql SQL语句，需要带 rownum rn 例如：select rownum rn,t.* from zzda t
	 *@param psize 每页记录数
	 * */
	@Override
	public Map<String, Object> selectPage(int pindex, String psql, int psize) {
		  Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("Pindex", pindex);
	       paramMap.put("Psql", psql);
	       paramMap.put("Psize", psize);
	       paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
	       paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
	       this.getSqlSession().select("com.gljx.web.dao.InsuranceRegMapper.pagemap", paramMap, null);
	       return  paramMap;
	}

	/**
	 * 取记录总数      dmz 20160921
	 * @param psql   SQL语句
	 * @return
	 */
	@Override
	public Map<String, Object> selectPageCount(String psql) {
		   Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("Psql", psql);
	       paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
	       this.getSqlSession().select("com.gljx.web.dao.InsuranceRegMapper.pagecount", paramMap, null);
	       return paramMap;
	}

	/**
	 * 根据条件查询子类信息
	 */
	@Override
	public List<InsuranceRegEntry> selectInsuranceRegEntry(String fparentId, String carName, String carNumber) {
		// TODO Auto-generated method stub
		   Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("v_fparentid", fparentId);
	       paramMap.put("v_carName", carName);
	       paramMap.put("v_carNumber", carNumber);
	       paramMap.put("v_cursor", oracle.jdbc.OracleTypes.CURSOR);
	       this.getSqlSession().select("com.gljx.web.dao.InsuranceRegEntryMapper.selectInsuranceRegEntry", paramMap, null);
	       @SuppressWarnings("unchecked")
		   List<InsuranceRegEntry> insuranceRegEntrys = (List<InsuranceRegEntry>) paramMap.get("v_cursor");
		   return insuranceRegEntrys;
	}

	/**
	 * 添加或修改
	 */
	@Override
	public void editInsuranceReg(InsuranceReg insuranceReg, String[] fseq, String[] cfcarnumber, String[] cfcarid,
			String[] cftype, String[] cfbizdate, String[] cfenddate, String[] cfamount, String[] cfremark, User user,
			String time) {
		// TODO Auto-generated method stub
		if("".equals(insuranceReg.getFid()) || insuranceReg.getFid() == null){
			//创建人id
			insuranceReg.setFcreatorid(user.getFid());
			//利用UUID生成fid
			insuranceReg.setFid(GetFid.UUIDIntoFid());
			//当前时间
			insuranceReg.setFcreatetime(time);
			//状态
            insuranceReg.setCfbillstate(3L);
            //添加
			insuranceRegMapper.insertSelective(insuranceReg);
		}else{
			//修改时间时间
			insuranceReg.setFlastupdatetime(time);
			//修改人id
			insuranceReg.setFlastupdateuserid(user.getFid());
			//修改
			insuranceRegMapper.updateByPrimaryKeySelective(insuranceReg);
			//先删除子类信息
			insuranceRegEntryMapper.deleteByFparentId(insuranceReg.getFid());
		}
		//添加子类
		List<InsuranceRegEntry> insuranceRegEntrys = new ArrayList<>();
		if(fseq == null || fseq.length == 0){
			
		}else{
			for (int i = 0; i < fseq.length; i++) {
				InsuranceRegEntry insuranceRegEntry = new InsuranceRegEntry();
				insuranceRegEntry.setCfcarnumber(cfcarnumber[i]);
				insuranceRegEntry.setCfcarid(cfcarid[i]);
				if("".equals(cftype[i])){
					
				}else{
					insuranceRegEntry.setCftype(Long.parseLong(cftype[i]));
				}
				insuranceRegEntry.setCfbizdate(cfbizdate[i]);
				insuranceRegEntry.setCfenddate(cfenddate[i]);
				if("".equals(cfamount[i])){
					
				}else{
					insuranceRegEntry.setCfamount(new BigDecimal(cfamount[i]));
				}
				insuranceRegEntry.setCfremark(cfremark[i]);
				insuranceRegEntry.setFseq((long)i+1);
				insuranceRegEntry.setFid(GetFid.UUIDIntoFid());
				insuranceRegEntry.setFparentid(insuranceReg.getFid());
				insuranceRegEntrys.add(insuranceRegEntry);
			}
			insuranceRegEntryMapper.insertInsuranceRegEntry(insuranceRegEntrys);
		}
	}

	/**
	 * 删除
	 */
	@Override
    public void deleteInsuranceRebByFid(String fid, String[] deleteId, String[] fnumber, String fnumber2) {
        // TODO Auto-generated method stub
		if(fid == null || "".equals(fid)){
            /*//先删除子类信息
            insuranceRegEntryMapper.deleteByArray(deleteId);
			//在删除父类信息
			insuranceRegMapper.deleteByArray(deleteId);*/
            for (int i = 0; i < deleteId.length; i++) {
                insuranceRegEntryMapper.deleteByFparentId(deleteId[i]);
                //在删除父类信息
                insuranceRegMapper.deleteByPrimaryKey(deleteId[i]);
                //删除流程
                activitiUtil.deleteTask(ActivitiKey.PUBLIC_KEY, fnumber[i]);
            }
		}else{
			//先删除子类信息
			insuranceRegEntryMapper.deleteByFparentId(fid);
			//在删除父类信息
			insuranceRegMapper.deleteByPrimaryKey(fid);
            //删除流程
            activitiUtil.deleteTask(ActivitiKey.PUBLIC_KEY, fnumber2);
        }
	}

	/**
	 * 根据fid查询
	 */
	@Override
	public InsuranceReg queryInsuranceRegByFid(String fid) {
		// TODO Auto-generated method stub
		return insuranceRegMapper.selectInsuranceRegByFid(fid);
	}

	/**
	 * 审核，反审核
	 */
	@Override
    public int auditInsuranceReg(String fid, String type, String fhandlerId, List<InsuranceRegEntry> insuranceRegEntrys, String message, String state, String fnumber) {
        // TODO Auto-generated method stub
		InsuranceReg insuranceReg = new InsuranceReg();
		//fid
		insuranceReg.setFid(fid);
		//操作人id
		insuranceReg.setFauditorid(fhandlerId);
        //修改人
        insuranceReg.setFlastupdateuserid(fhandlerId);
        Axamine axamine = new Axamine();
        if ("1".equals(type)) {
            ServerResponse serverResponse = axamine.auditProcess(fnumber, state, message);
            insuranceReg.setCfbillstate(5L);
            if (serverResponse.getStatus() == 1) {
                for (InsuranceRegEntry insuranceRegEntry : insuranceRegEntrys) {
                    if (insuranceRegEntry.getCfcarid() == null || "".equals(insuranceRegEntry.getCfcarid())) {

                    } else {
                        Car car = new Car();
                        car.setFid(insuranceRegEntry.getCfcarid());
                        //获取类型
                        long type2 = insuranceRegEntry.getCftype();
                        //获取办理时间
                        String blrq = insuranceRegEntry.getCfbizdate();
                        //获取到期时间
                        String dqrq = insuranceRegEntry.getCfenddate();
                        if (type2 == 1) {
                            car.setCfnjrq(blrq);
                            car.setCfnjdqr(dqrq);
                        } else if (type2 == 2) {
                            car.setCfjqxrq(blrq);
                            car.setCfjqxdqr(dqrq);
                        } else if (type2 == 3) {
                            car.setCfjqxrq(blrq);
                            car.setCfjqxdqr(dqrq);
                        } else if (type2 == 4) {
                            car.setCfejwhrq(blrq);
                            car.setCfejwhdqr(dqrq);
                        } else if (type2 == 5) {
                            car.setCfzjrq(blrq);
                            car.setCfzjdqr(dqrq);
                        } else if (type2 == 6) {
                            car.setCfkmstxzrq(blrq);
                            car.setCfkmstxzdqr(dqrq);
                        } else {

                        }
                        if (blrq == null && dqrq == null) {

                        } else {
                            carMapper.updateByPrimaryKeySelective(car);
                        }
                    }
                }
                return insuranceRegMapper.updateByPrimaryKeySelective(insuranceReg);
            } else if (serverResponse.getStatus() == 2) {
                //推进
                if (!"1".equals(state)) {
                    insuranceReg.setFdescription(message);
                    insuranceReg.setCfbillstate(3L);
                    insuranceRegMapper.updateByPrimaryKeySelective(insuranceReg);
                    return 2;
                }
            } else {
                //错误
                return 0;
            }
        }else{
            axamine.againstAuditProcess(fnumber, message);
            insuranceReg.setCfbillstate(3L);
            return insuranceRegMapper.updateByPrimaryKeySelective(insuranceReg);
        }
        return 0;
    }

    @Override
    public int modifyInsuranceReg(InsuranceReg insuranceReg) {
        return insuranceRegMapper.updateByPrimaryKeySelective(insuranceReg);
    }

    @Override
    public List<InsuranceRegEntry> selectInsuranceRegByInfo(String fcompanyid, String carName, String carNumber, String beginTime, String endTime) {
        Map<String, String> map = new HashMap<>();
        map.put("fcompanyid", fcompanyid);
        map.put("carName", carName);
        map.put("carNumber", carNumber);
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        return insuranceRegEntryMapper.selectInsuranceRegByInfo(map);
    }
}
